<?php
/**
 ***********************************************************************************************************************
 * 获取申通E3系统基础快递员信息
 * @author llq  2015/5/20
 ***********************************************************************************************************************
 */
namespace soa\sto;
use model\parter\emp\emp;
use soa\client;

/**
 * Class courier
 * @package soa\sto
 */
class courier extends client
{
    /**
     * 服务地址
     * @var string
     */
    public $service_url = 'http://express.core.kuaidihelp.com/';

    /**
     * 服务名称
     * @var string
     */
    protected $service_name = 'kuaidihelp_express';

    /**
     * 根据手机号码获取快递员信息
     * @param $mobile
     * @return array|bool
     */
    public function getCourierByMobile($mobile)
    {
        if(empty($mobile) || !is_numeric($mobile) || strlen($mobile) != 11) {
            return array();
        }
        return $this->get('v1/parter/sto/getCourierByMobile', array('mobile' => $mobile));
    }

    /**
     * 根据工号获取快递员信息
     * @param $emp_no
     * @return array|bool
     */
    public function getCourierByEmpNo($emp_no)
    {
        if(empty($emp_no) || !is_string($emp_no)) {
            return array();
        }
        return $this->get('v1/parter/sto/getCourierByEmpNo', array('emp_no' => $emp_no));
    }

    /**
     * 根据多个手机号码获取快递员信息
     * @param array $mobiles
     * @return array|bool
     */
    public function getCourierByMobiles(array $mobiles)
    {
        if(empty($mobiles)) {
            return array();
        }
        return $this->post('v1/parter/sto/getCourierByMobiles', array('mobiles' => implode(',', $mobiles)));
    }

    /**
     * 根据多个工号获取快递员信息
     * @param array $emp_nos
     * @return array|bool
     */
    public function getCourierByEmpNos(array $emp_nos)
    {
        if(empty($emp_nos)) {
            return array();
        }
        return $this->post('v1/parter/sto/getCourierByEmpNos', array('emp_nos' => implode(',', $emp_nos)));
    }

    /**
     * 根据网点编号批量获取快递员
     * @param $branch_id
     * @return array|bool
     */
    public function getCourierByBranchId($branch_id)
    {
        if(empty($branch_id)) {
            return array();
        }
        return $this->post('v1/parter/sto/getCourierByBranchId', array('branch_id' => $branch_id));
    }

    /**
     * 根据网点名称批量获取快递员
     * @param $branch_id
     * @return array|bool
     */
    public function getCourierByBranchName($branch_name)
    {
        if(empty($branch_name)) {
            return array();
        }
        return $this->post('v1/parter/sto/getCourierByBranchName', array('branch_name' => $branch_name));
    }

    /**
     * 新增未读订单订单（写入redis）
     * @param int $userId   用户id
     * @param array $orderInfo  订单信息array(订单编号,时间戳)
     * @return bool
     */
    public function addUnReadOrders($userId,$orderInfo)
    {
        if(empty($userId) || empty($orderInfo)){
            return false;
        }
        $key = 'unReadOrders';
        $hashKey = 'courier:'.$userId;
        $unReadOrders = \artisan\cache::hget($key,$hashKey);                //获取用户未读订单
        $unReadOrdersArr = array();
        if($unReadOrders && ($unReadOrders != 'null')){
            $unReadOrdersArr = json_decode($unReadOrders,true);
            if($this->checkOrderIdIsset($unReadOrdersArr,$orderInfo) == false){ //该订单是否被添加过
                return false;
            }
            array_push($unReadOrdersArr,$orderInfo);
        }else{
            $unReadOrdersArr[1] = $orderInfo;                               //从数组1开始插入json格式为{[]}，若为0，json格式为[]
        }
        $unReadOrdersNew = json_encode($unReadOrdersArr);
        $result = \artisan\cache::hset($key,$hashKey,$unReadOrdersNew);     //新添加未读订单
        if($result !=1 && $result!=0){
            return false;
        }
        return true;
    }

    /**
     *  查询订单是否存在
     * @param $unReadOrdersArr
     * @param $orderInfo
     */
    public function checkOrderIdIsset($unReadOrdersArr,$orderInfo)
    {
        foreach ($unReadOrdersArr as $key=>$unReadOrder){
            if($orderInfo[0] == $unReadOrder[0]){
                return false;
            }
        }
        return true;
    }

    /**
     * 更新未读订单数量
     * @param int $userId   用户id
     * @param string $orderNum  订单编号
     * @return bool
     */
    public function updateUnReadOrders($userId,$orderNum){
        if(empty($userId) || empty($orderNum)){
            return false;
        }
        $key = 'unReadOrders';
        $hashKey = 'courier:'.$userId;
        $unReadOrders = \artisan\cache::hget($key,$hashKey);                        //获取该用户未读订单
        if($unReadOrders){
            $unReadOrdersArr = json_decode($unReadOrders,true);
            foreach ($unReadOrdersArr as $orderKey => $unReadOrder){
                    if(in_array($orderNum,$unReadOrder)){
                    unset($unReadOrdersArr[$orderKey]);                             //移除已读订单
                }
            }
            $unReadOrdersNew = empty($unReadOrdersArr)?'':json_encode($unReadOrdersArr);
            if($unReadOrders != $unReadOrdersNew){                                  //只有未读订单有变更时才更新
                $result = \artisan\cache::hset($key,$hashKey,$unReadOrdersNew);     //更新未读订单
                logDebug('key:'.$key.'--hashKey:'.$hashKey.'--result:'.$result,'updateUnReadOrders');//'--unReadOrdersNew:'.$unReadOrdersNew.'--unReadOrders:'.$unReadOrders.
                if($result !=1 && $result!=0){
                    return false;
                }
            }
        }
        return true;
    }
}